package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootReward;
import org.footballmanager.pandafootball.system.form.impl.FootRewardSearchForm;
import org.footballmanager.pandafootball.system.service.FootRewardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footRewardSearchForm")
@RequestMapping("/foot/footReward")
public class FootRewardController extends AddAndEditController<FootReward> {
    /* 实体名称 */
    private static final String ENTITYNAME = "footReward";
    private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootRewardService footRewardService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     *
     * @param map                 参数
     * @param footRewardSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootRewardSearchForm footRewardSearchForm) {
        FootRewardSearchForm newFootRewardSearchForm = new FootRewardSearchForm();
        if (footRewardSearchForm != null) {
            newFootRewardSearchForm = footRewardSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootRewardSearchForm);
    }

    /**
     * 抽奖记录列表
     *
     * @param map                 参数
     * @param footRewardSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootRewardSearchForm footRewardSearchForm) {
        footRewardService.search(ENTITYNAME, map, footRewardSearchForm);
        return LISTPAGE;
    }

    /**
     * 删除抽奖记录记录
     *
     * @param id      主键
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footRewardService.delete(id, request);
        return REDIRECT;
    }

    /**
     * 删除抽奖记录列表
     *
     * @param request             请求
     * @param footRewardSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootRewardSearchForm footRewardSearchForm) {
        footRewardService.deleteAll(footRewardSearchForm, request);
        return REDIRECT;
    }

    /**
     * 打开编辑抽奖记录页面
     *
     * @param map 参数
     * @param id  主键
     * @return 页面
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footRewardService.get(id));
        return EDITPAGE;
    }

    /**
     * 浏览抽奖记录页面
     *
     * @param map                 参数
     * @param id                  主键
     * @param footRewardSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootRewardSearchForm footRewardSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footRewardSearchForm);
        map.addAttribute(ENTITYNAME, footRewardService.get(id));
        return VIEWPAGE;
    }

}
